package com.wuming.house.service.impl;

import com.wuming.house.dao.GoodsCateMapper;
import com.wuming.house.model.GoodsCate;
import com.wuming.house.service.GoodsCateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuhuai
 * @createTime 2019/8/16 14:33
 * @Description:
 */
@Service("goodsCateService")
public class GoodsCateServiceImpl extends BaseServiceImpl implements GoodsCateService {

    @Autowired
    private GoodsCateMapper goodsCateMapper;

    @Override
    public List<GoodsCate> getGoodsCatesByPage(String name,int pageIndex,int pageSize) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)){
            params.put("name",name);
        }
        try {
            return (List<GoodsCate>) this.getPageList(GoodsCateMapper.class,"selectGoodsCatesByParams",params,pageIndex,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据库查询分类列表失败,失败信息:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public int getTotal(String name) {
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)){
            params.put("name",name);
        }
        try {
            return this.getTotal(GoodsCateMapper.class,"selectTotal",params);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("数据库查询分类总数失败,失败信息:{}",e.getMessage());
        }
        return 0;
    }

    @Override
    public void save(GoodsCate goodsCate) {
        goodsCateMapper.insert(goodsCate);
    }

    @Override
    public void modifyGoodsCate(GoodsCate goodsCate) {
        goodsCateMapper.updateGoodsCate(goodsCate);
    }

    @Override
    public GoodsCate getGoodsCateById(Integer id) {
        try {
            return goodsCateMapper.selectGoodsCateById(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据库插入发生异常,异常信息:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public List<GoodsCate> getGoodsCatesByPId(Integer pId) {
        Map<String, Object> params = new HashMap<>();
        if (null != pId) {
            params.put("pId", pId);
        }
        try {
            return goodsCateMapper.selectGoodsCatesByParams(params);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据库查询发生异常,异常信息:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public void delete(Integer id) {
        goodsCateMapper.delete(id);
    }
}
